package com.mmlc.meimei.lichengkongjian.classify;

import android.view.View;
import android.widget.TextView;

import com.androidnetframe.httputil.BaseRequestFilterLayer;
import com.androidnetframe.responsejavabean.RequestError;
import com.mmlc.inter.InterActivity;
import com.mmlc.meimei.lichengkongjian.R;
import com.mmlc.network.filter.GetPaymentRequestFilter;
import com.mmlc.network.responsebean.AddressDetailResponseBean;
import com.mmlc.network.responsebean.GetPaymentResponseBean;
import com.mmlc.network.responsebean.SubmitResponseBean;
import com.mmlc.utils.ActivityManager;
import com.mmlc.utils.ViewUtils;

import java.util.List;

/**
 * description: 订单详情
 * by king
 * Created at 2016/8/3 11:15
 */
public class OrderInformationActivity extends InterActivity {

    @Override
    protected int initContentView() {
        return R.layout.activity_order_information;
    }

    SubmitResponseBean.DataBean order;
    String order_id, order_price, companyName = "";
    int modeInvoice = 0;
    String pay_code;
    AddressDetailResponseBean.DataBean address;

    @Override
    protected void initExtra() {
        super.initExtra();
        order = (SubmitResponseBean.DataBean) getIntent().getSerializableExtra("order");
        if (order != null) {
            order_id = order.getOrder_id();
            order_price = order.getOrder_price();
            pay_code = order.getPay_code();
        }
        modeInvoice = getIntent().getIntExtra("modeInvoice", 0);
        if (modeInvoice == 2) {
            companyName = getIntent().getStringExtra("companyName");
        }

        address = (AddressDetailResponseBean.DataBean) getIntent().getSerializableExtra("address");
    }

    TextView tv_name, tv_phone, tv_address, tv_pay_content, tv_invoice_content, tv_order_number, tv_price;

    @Override
    protected void initView() {
        tv_name = ViewUtils.getView(activity, R.id.tv_name);
        tv_phone = ViewUtils.getView(activity, R.id.tv_phone);
        tv_address = ViewUtils.getView(activity, R.id.tv_address);
        tv_pay_content = ViewUtils.getView(activity, R.id.tv_pay_content);
        tv_invoice_content = ViewUtils.getView(activity, R.id.tv_invoice_content);
        tv_order_number = ViewUtils.getView(activity, R.id.tv_order_number);
        tv_price = ViewUtils.getView(activity, R.id.tv_price);

        tv_name.setText(address.getContactor());
        tv_phone.setText(address.getPhone());
        tv_address.setText(address.getProvince_name() + "/" + address.getCity_name() + "/" + address.getArea_name() + "/" + address.getAddress());
        tv_invoice_content.setText(modeInvoice == 0 ? "暂不开具发票" : (modeInvoice == 1 ? "开具个人发票" : (companyName.length() > 8 ? companyName.substring(0, 8) + "..." : companyName)));
        tv_order_number.setText(order_id);
        tv_price.setText("¥ " + order_price);
    }

    @Override
    protected void initInfo() {
        super.initInfo();

        GetPaymentRequestFilter filter = new GetPaymentRequestFilter(activity);
        filter.sendRequest(new BaseRequestFilterLayer.AccessResultFromFilter<GetPaymentResponseBean>() {
            @Override
            public void onSuccess(GetPaymentResponseBean result) {
                List<GetPaymentResponseBean.DataBean> data = result.getData();
                for (int i = 0; i < data.size(); i++) {
                    if (data.get(i).getPay_code().equals(pay_code)) {
                        tv_pay_content.setText(data.get(i).getPay_name());
                        break;
                    }
                }
            }

            @Override
            public void onFailed(View errorLayout, RequestError serverError, int failedType) {
                initInfo();
            }
        });
    }

    @Override
    protected void initEvent() {
        ViewUtils.getView(activity, R.id.iv_back).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.iv_back:
                ActivityManager.finishActivity(ProductInfoActivity.class);
                ActivityManager.finishActivity(OrderFormActivity.class);
                ActivityManager.finishActivity(SubmitSuccessActivity.class);
                finish();
                break;
        }
    }

}
